<template>
  <div id="deptTree" v-cloak>
    <el-tree :props="defaultProps" :load="loadChildrenNode" lazy :default-expanded-keys="default_expended_key" highlight-current node-key="id" :expand-on-click-node="false" ref="tree" accordion @node-click="handleNodeClick">
    </el-tree>
  </div>
</template>
<script>
import { getToken, setToken, removeToken } from '@/util/auth'
import request from '@/router/axios'
import { fetchDeptTree } from '@/api/role'

var defaultNode = ''
export default {
  name: 'deptTree',
  data() {
    return {
      default_expended_key: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  props: ['deptProperty'],
  methods: {
    // 初始化树
    loadChildrenNode(node, resolve) {
      if (node.level === 0) {
        request.get('/admin/community?getCommunitysByCurrentUser').then(res => {
          if (res.status === 200) {
            defaultNode = res.data[0].id
            this.$refs.tree.setCurrentKey(defaultNode)
            return resolve(res.data)
          }
        })
      } else {
        //加载企业节点
        let para = {
          params: {
            parentId: node.key,
            deptProperty: node.data.type
          }
        }
        request.get('/admin/dept/getDeptNodeByParent', para).then(res => {
          if (res.status === 200) {
            return resolve(res.data)
          }
        })
      }
    },
    handleNodeClick(data, node) {
      this.$emit('treeNode-click', data, node)
    }
  }
}
</script>

</html>
